package org.itboys.mobile.controller;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.PearlAccount;
import org.itboys.mobile.service.mongo.mobile.PearlAccountService;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created 2017/10/16.
 * @author 朱坤涛
 */
@Controller
@RequestMapping("/pearl/mobile/account")
public class PearlMobileAccountController extends BaseController{
    @Autowired
    private PearlAccountService pearlAccountService;
    @ApiOperation(value = "添加账户")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public void save(
            @ApiParam(required = true, name = "memberId", value = "会员Id") @RequestParam(value = "memberId", required = true) Long memberId,
            @ApiParam(required = true, name = "type", value = "账户类型(1-支付宝 2-银行卡)") @RequestParam(value = "type", required = true) Integer type,
            @ApiParam(required = true, name = "account", value = "账户号") @RequestParam(value = "account", required = true) String account,
            @ApiParam(required = true, name = "name", value = "账户名") @RequestParam(value = "name", required = true) String name,
            @ApiParam(required = false, name = "bank", value = "开户银行") @RequestParam(value = "bank", required = false) String bank,
            @ApiParam(required = false, name = "subBankName", value = "开户支行") @RequestParam(value = "subBankName", required = false) String subBankName,
            HttpServletRequest request, HttpServletResponse response) {
        try {
            PearlAccount act = new PearlAccount();
            act.setMemberId(memberId);
            act.setAccount(account);
            act.setName(name);
            act.setType(type);
            act.setSubBankName(subBankName);
            if(act.getType() == 1) {
                act.setName("支付宝");
            } else {
                act.setName(name);
            }
            pearlAccountService.save(act);
            AjaxUtils.renderJson(response, ResultPageDOHelper.getMsgResultDO(account, "success"));
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJson(response, ResultPageDOHelper.getErrorResultDO(-1, "系统异常!"));
        }
    }

    //获取账户列表
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public void list(HttpServletRequest request,
                     HttpServletResponse response,
                     @RequestParam(value = "memberId",required = true)Long memberId,
                     @Param("callback") String callback){
            try {
                Map<String,Object> param = ParamUtil.getParamInt(request,"memberId");
                Page<PearlAccount> page = pearlAccountService.pageQuery(param,request);
                AjaxUtils.renderJsonporJson(response,callback,ResultPageDOHelper.getResultPageDO(page.getTotal(),page.getData()));
            }catch (Exception e){
                e.printStackTrace();
                AjaxUtils.renderJsonporJson(response,callback,ResultPageDOHelper.getMsgResultDOJx("失败！"+e.getMessage()));
            }
    }

    //设置默认账户
    @RequestMapping(value = "/setDefault",method = RequestMethod.POST)
    public void serDefault(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "memberId",required = true)Long memberId,
            @RequestParam(value = "accountId",required = true)Long accountId,
            @Param("callback")String callback){
        try {
            pearlAccountService.setDefaultAccount(accountId,memberId);
            AjaxUtils.renderJsonporJson(response,callback,ResultPageDOHelper.getMsgResultDOJx("成功"));
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response,callback,ResultPageDOHelper.getMsgResultDOJx("失败！"+e.getMessage()));
        }
    }

    //获取默认账户
    @RequestMapping(value = "/getDefault",method = RequestMethod.POST)
    public void getDefault(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "memberId",required = true)Long memberId,
            @Param("callback")String callback){
        try {
            PearlAccount account = pearlAccountService.getDefaultAccount(memberId);
            AjaxUtils.renderJsonporJson(response,callback,ResultPageDOHelper.getMsgResultDO(account,"成功"));
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response,callback,ResultPageDOHelper.getMsgResultDOJx("失败！"+e.getMessage()));
        }
    }

    //删除账户
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public void delete(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "accountId",required = true)Long accountId,
            @Param("callback")String callback){
        try {
            pearlAccountService.deleteAccount(accountId);
            AjaxUtils.renderJsonporJson(response,callback,ResultPageDOHelper.getMsgResultDOJx("成功"));
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response,callback,ResultPageDOHelper.getMsgResultDOJx("失败1"+e.getMessage()));
        }
    }
}
